package com.itany.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.constant.UserConstant;
import com.itany.exception.DisabledException;
import com.itany.exception.EnabledException;
import com.itany.pojo.User;
import com.itany.pojo.UserExample;
import com.itany.service.UserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.itany.mapper.UserMapper;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户服务impl
 *
 * @author Tom
 * @date 2021/06/26
 */
@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo<User> findUserAll(Integer page, Integer rows) {

        PageHelper.startPage(page,rows);
        UserExample example = new UserExample();
        List<User> users = userMapper.selectByExample(example);
        PageInfo<User> info = new PageInfo<>(users);
        return info;
    }

    @Override
    public void modifyById(Integer id, String username, String phone, String email, String interest) {

        User user = new User();
        user.setId(id);
        user.setUsername(username);
        user.setPhone(phone);
        user.setEmail(email);
        user.setInterest(interest);
        userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public void modifyStatusDisable(Integer id) throws DisabledException {
        User user = userMapper.selectByPrimaryKey(id);
        User user1 = new User();
        user1.setId(id);
        if (user.getFlag().equals(UserConstant.USER_ENABLED_STATE)) {
            user1.setFlag(UserConstant.USER_DISABLED_STATE);
        } else if (user.getFlag().equals(UserConstant.USER_DISABLED_STATE)){
            throw new DisabledException("已禁用");
        }
        userMapper.updateByPrimaryKeySelective(user1);
    }

    @Override
    public void modifyStatusEnable(Integer id) throws EnabledException {
        User user = userMapper.selectByPrimaryKey(id);
        User user1 = new User();
        user1.setId(id);
        if (user.getFlag().equals(UserConstant.USER_DISABLED_STATE)) {
            user1.setFlag(UserConstant.USER_ENABLED_STATE);
        } else if (user.getFlag().equals(UserConstant.USER_ENABLED_STATE)){
            throw new EnabledException("已启用");
        }
        userMapper.updateByPrimaryKeySelective(user1);
    }

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public PageInfo<User> findByName(Integer page, Integer rows, String keyword, Integer searchType) {
        PageHelper.startPage(page, rows);
//        User user = new User();
        List<User> users = new ArrayList<>();
        if (searchType == 0 && StringUtils.isEmpty(keyword)) {
            users = userMapper.selectByAll(null);
        } else if (StringUtils.isEmpty(keyword)) {
            users = userMapper.selectByFlag(searchType);
        } else if (searchType == 0 && !StringUtils.isEmpty(keyword)) {
            users = userMapper.selectByUsernameLike(keyword);
        } else if (!StringUtils.isEmpty(keyword)) {
            users = userMapper.selectByUsernameLikeAndFlag(keyword, searchType);
        }
        PageInfo<User> info = new PageInfo<>(users);
        return info;

    }


}
